<template>
    <div class="login_container">
        <div class="login_box">
            <div class="avatar_box">
                <img src="@/assets/logo.png" alt="">
            </div>
            <el-form  label-width="0px" :model="loginModel" :rules="loginRules" ref="loginFormRef" class="form_box bs">
                <el-form-item prop="username">
                    <el-input prefix-icon="iconfont icon-user"  v-model="loginModel.username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input prefix-icon="iconfont icon-mima1" v-model="loginModel.password" type="password"></el-input>
                </el-form-item>
                <el-form-item class="btn">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script lang="ts">
    import {apiAddress} from "@/utils/apiAddress";
    import {Component,Vue, Provide} from "vue-property-decorator";
    @Component({})
    export default class Login extends Vue{
        loginModel:{
            username:String;
            password:String;
        }={
            username:"admin",
            password:"123456"
        };
        loginRules={
            username:[
                { required: true, message: '请输入用户名', trigger: 'blur' },
                { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
            ],
            password:[
                { required: true, message: '请输入密码', trigger: 'blur' },
                { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
            ]
        }

        login():void{
            (this.$refs.loginFormRef as any).validate(async (validate:boolean)=>{
                if(!validate) return
                const res=await apiAddress.login(this.loginModel);
                if(res.meta.status!==200) return (this as any).$message({message: res.meta.msg,type: 'error'});
                (this as any).$message({message: res.meta.msg,type: 'success'});
                window.sessionStorage.setItem("token",res.data.token)
                this.$router.push({path:"/home"})
            })
        }
        resetForm():void{
            (this.$refs.loginFormRef as any).resetFields()
        }
    }
</script>


<style lang="scss" scoped>
.login_container{
    background-color: #2b4b6b;
    position: relative;
    height: 100%;
    .login_box{
        background-color: #ffffff;
        width: 450px;
        height: 300px;
        position: absolute;
        border-radius: 3px;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
        -moz-transform: translate(-50%,-50%);
        -webkit-transform: translate(-50%,-50%);
        .avatar_box{
            width: 130px;
            height: 130px;
            border-radius: 50%;
            border: solid 1px #eee;
            box-shadow: 0 0 10px #ddd;
            padding: 10px;
            position: absolute;
            left: 50%;
            transform: translate(-50%,-50%);
            -moz-transform: translate(-50%,-50%);
            -webkit-transform: translate(-50%,-50%);
            background-color: #ffffff;
            img{
                width: 100%;
                height: 100%;
                border-radius: 50%;
                background-color: #eee;
            }
        }
        .form_box{
            position: absolute;
            bottom: 0;
            width: 100%;
            padding: 0 20px;
        }
        .btn{
            display: flex;
            display: -webkit-flex;
            justify-content: flex-end;
        }
    }
}
</style>